package hz.mobileproject.pda.firesafety.view;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import hz.mobileproject.pda.R;
import hz.mobileproject.pda.firesafety.bean.FireSafetyThingBean;

import static hz.mobileproject.pda.R.id.btn_pop_cancel;

/**
 * Created by hasee on 2018/1/11.
 */

public class FireSafetyUpdatePopWindow implements View.OnClickListener{

    private PopupWindow mPopupwindow;
    private TextView mTvIsOut;
    private TextView mTvName;
    private EditText mEtContent;
    private TextView mTvSave;
    private TextView mTvCancel;
    private Context mContext;
    private String[] strArr = {"过期", "未过期"};
    private String mstrIsOut;
    private FireSafetyCallback mCallback;
    private String mstrAssetCode;

    public FireSafetyUpdatePopWindow(Context context, FireSafetyThingBean safetyThingBean, FireSafetyCallback callback){
        mContext = context;
        mstrAssetCode = safetyThingBean.getAssetCode();
        mCallback = callback;
        View view = LayoutInflater.from(context).inflate(R.layout.pop_fire_toupdate, null);
        mTvIsOut = (TextView) view.findViewById(R.id.tv_is_guoqi);
        mTvName = (TextView) view.findViewById(R.id.tv_qicai);
        mEtContent = (EditText) view.findViewById(R.id.et_err_pop_question);
        mTvSave = (TextView) view.findViewById(R.id.btn_pop_save);
        mTvCancel = (TextView) view.findViewById(btn_pop_cancel);
        mTvSave.setOnClickListener(this);
        mTvCancel.setOnClickListener(this);
        mTvIsOut.setOnClickListener(this);

        mPopupwindow = new PopupWindow(context);
        mPopupwindow.setContentView(view);
        mPopupwindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        mPopupwindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        mPopupwindow.setBackgroundDrawable(new BitmapDrawable());
        mPopupwindow.setOutsideTouchable(false);
        mPopupwindow.setFocusable(true);

        mTvName.setText(safetyThingBean.getAssetCode());
    }

    public void showPopupwindow(View view){
        mPopupwindow.showAtLocation(view, Gravity.CENTER,0,0);
    }

    public boolean isShow(){
        return  mPopupwindow.isShowing();
    }

    public void cancel(){
        if(isShow()){
            mPopupwindow.dismiss();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_pop_cancel:
                cancel();
                break;
            case R.id.btn_pop_save:
                if (mCallback != null){
                    mCallback.onSave(mstrAssetCode, mstrIsOut + ":" + mEtContent.getText().toString());
                    cancel();
                }
                break;
            case R.id.tv_is_guoqi:
                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setItems(strArr,new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mstrIsOut = strArr[which];
                        mTvIsOut.setText(mstrIsOut);
                    }
                });
                builder.create().show();
                break;
            default:
                break;
        }
    }

    public interface FireSafetyCallback{
        public void onSave(String assetCode, String condition);
    }
}
